package com.controller;

import com.mapper.SystemMessageMapper;
import com.mapper.UserMapper;
import com.mapper.UserQuestionsMapper;
import com.message.UserMessage;
import com.model.*;
import com.util.ConstantUtil;
import com.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@PropertySource("classpath:message.properties")
public class SystemMessageController {



	@Autowired
	private SystemMessageMapper systemMessageMapper;


	@Autowired
	private UserQuestionsMapper userQuestionsMapper;

	@Autowired
	private Environment env;




	@RequestMapping("/systemMessageReceive")
	public String systemMessage(Map<String, Object> model, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);
		String type = request.getParameter("type");

		SystemMessage message = new SystemMessage();
		message.setUserId(user.getUserId());
		message.setType(1);
		List<SystemMessage> list = systemMessageMapper.getAllMessage( message );
		List<SystemMessage> unreadList = list.stream().filter(item -> (item.getStatus()==0)).collect(Collectors.toList());
		model.put("pagename","systemMessageReceive");
		model.put("list",list);
		model.put("unreadmessagenumber",unreadList.size());
		return "usercenter";
	}


	@RequestMapping("/questionWrite")
	public String systemMessageWrite(Map<String, Object> model, HttpServletRequest request) {
		model.put("pagename","questionWrite");
		return "usercenter";
	}


	@RequestMapping("/addQuestionWrite")
	public String addQuestionWrite(Map<String, Object> model, HttpServletRequest request) {

		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String questionId = request.getParameter("questionId");

		User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);

		UserQuestions question = new UserQuestions();
		question.setUserId(user.getUserId());
		question.setQuestionTitle(title);
		question.setAnswer(content);

		if(StringUtils.isNotBlank(questionId)){
			question.setQuestionId(Integer.parseInt(questionId));
		}

		model.put("pagename","questionWrite");
		return "usercenter";
	}


	@RequestMapping("/addNewQuestion")
	@ResponseBody
	public ResponseJsonMsg addNewQuestion(HttpServletRequest request) {

		String questionId = request.getParameter("questionId");
		String newQuestion = request.getParameter("newQuestion");
		String questionTitle = request.getParameter("questionTitle");

		ResponseJsonMsg msg = new ResponseJsonMsg();
		User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);
		UserQuestions question = new UserQuestions();
		question.setUserId(user.getUserId());

		if(StringUtils.isNotBlank(newQuestion)){
			question.setQuestionContent(newQuestion);
		}

		if(StringUtils.isNumeric(questionId)){
			question.setQuestionId(Integer.parseInt(questionId));
		}
		if(StringUtils.isNotBlank(questionTitle)){
			question.setQuestionTitle(questionTitle);
		}

		userQuestionsMapper.addUserQuestion(question);
		msg.setStatus(ResponseJsonMsg.success);
		return msg;
	}





	@RequestMapping("/questionDialog")
	public String questionDialog(Map<String, Object> model, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);

		UserQuestions questions= new UserQuestions();
		questions.setUserId(user.getUserId());

		List<UserQuestions>  list= userQuestionsMapper.getQuestionsByConditon(questions);

		List<UserQuestions> unreadList = list.stream().filter(item -> (item.getStatus()==0)).collect(Collectors.toList());
		model.put("unreadmessagenumber",unreadList.size());
		model.put("pagename","questionDialog");
		model.put("list",list);
		return "usercenter";
	}


	@RequestMapping("/getUseRquestionDialog")
	@ResponseBody
	public ResponseJsonMsg getUseRquestionDialog(Map<String, Object> model,@RequestBody Map map, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);
		ResponseJsonMsg msg = new ResponseJsonMsg();
		String questionId = map.get("questionId").toString();
		System.out.println(questionId+"-----------------");
		if(StringUtils.isBlank(questionId)){
			return msg;
		}

		UserQuestions questions= new UserQuestions();
		questions.setUserId(user.getUserId());

		questions.setQuestionId(Integer.parseInt(questionId));

		List<UserQuestions>  list= userQuestionsMapper.getQuestionsByConditon(questions);

		msg.setJsonData(list);
		return msg;
	}



	@RequestMapping("/updateQuestion")
	@ResponseBody
	public ResponseMsg updateQuestion(Map<String, Object> model, HttpSession session,@RequestParam(required = true) int userQuestionId) {
		User user = (User) session.getAttribute(ConstantUtil.loginSession);
		ResponseMsg msg = new ResponseMsg();
		UserQuestions question = new UserQuestions();
		question.setId(userQuestionId);
		userQuestionsMapper.updateUserQuestion(question);
		msg.setStatus(ResponseMsg.success);
		return msg;
	}


	@RequestMapping("/updateSystemMessage")
	@ResponseBody
	public ResponseMsg updateSystemMessage(Map<String, Object> model, HttpSession session,@RequestParam(required = true) int systemMessageId) {
		User user = (User) session.getAttribute(ConstantUtil.loginSession);
		ResponseMsg msg = new ResponseMsg();
		UserQuestions question = new UserQuestions();
		question.setId(systemMessageId);
		msg.setStatus(ResponseMsg.success);
		return msg;
	}




	public Environment getEnv() {
		return env;
	}

	public void setEnv(Environment env) {
		this.env = env;
	}


	public SystemMessageMapper getSystemMessageMapper() {
		return systemMessageMapper;
	}

	public void setSystemMessageMapper(SystemMessageMapper systemMessageMapper) {
		this.systemMessageMapper = systemMessageMapper;
	}


	public UserQuestionsMapper getUserQuestionsMapper() {
		return userQuestionsMapper;
	}

	public void setUserQuestionsMapper(UserQuestionsMapper userQuestionsMapper) {
		this.userQuestionsMapper = userQuestionsMapper;
	}
}
